home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 58606 / 58606.xpi / chrome / translator.jar / content / preferences.js < prev    next >
Text File  |  2010-01-19  |  7KB  |  212 lines

  1.  
  2. (function(namespace, $)
  3. {
  4.     namespace.Preferences = function(prefsManager)
  5.     {
  6.         this.prefs = prefsManager;
  7.     };
  8.     
  9.     namespace.Preferences.prototype = {
  10.         prefs: null,
  11.         
  12.         init: function()
  13.         {
  14.             // setup interface event listeners
  15.             this.setupEventListeners();
  16.             
  17.             // fill ui with data
  18.             this.fillUI();
  19.         },
  20.         
  21.         setupEventListeners: function()
  22.         {
  23.             // window listeners (to detect if any options were changed)
  24.             $(window).bind('command.translator input.translator', function(e) {
  25.                 if($(e.originalTarget).hasClass('translator-preference')) {
  26.                     // enable apply button
  27.                     $('#translator-preferences-button-apply').boolean('disabled', false);
  28.                 }
  29.             }.bind(this));
  30.             
  31.             // hotkey checkbox listener
  32.             $('#translator-preferences-translate-hotkey').bind('command.translator', function(e) {
  33.                 $('#translator-preferences-hotkey, #translator-preferences-hotkey-note').boolean('disabled', (!$(e.target).boolean('checked')));
  34.             });
  35.             
  36.             // hotkey textbox listeners
  37.             $('#translator-preferences-hotkey')
  38.                 .bind('focus.translator', function(e) {
  39.                     e.target.select();
  40.                 })
  41.                 .bind('keypress.translator', function(e) {
  42.                     var modifiers = [];
  43.                     var hotkey = String.fromCharCode(e.which).toUpperCase();
  44.                     
  45.                     if(e.ctrlKey) {
  46.                         modifiers.push('control');
  47.                     }
  48.                     if(e.altKey) {
  49.                         modifiers.push('alt');
  50.                     }
  51.                     if(e.shiftKey) {
  52.                         modifiers.push('shift');
  53.                     }
  54.                     /*
  55.                     if(e.metaKey) {
  56.                         modifiers.push('meta');
  57.                     }
  58.                     */
  59.                     modifiers = modifiers.join(' ');
  60.                     
  61.                     $(e.target)
  62.                         .attr('hotkeymodifiers', modifiers)
  63.                         .attr('hotkey', hotkey)
  64.                         .val(this.formatHotKey(modifiers, hotkey));
  65.                     
  66.                     e.stopPropagation();
  67.                 }.bind(this));
  68.             
  69.             
  70.             /* button listeners */
  71.             
  72.             // ok button
  73.             $('#translator-preferences-button-ok').bind('command.translator', function(e) {
  74.                 this.savePreferences();
  75.                 
  76.                 window.close();
  77.             }.bind(this));
  78.             
  79.             // apply button
  80.             $('#translator-preferences-button-apply').bind('command.translator', function(e) {
  81.                 this.savePreferences();
  82.                 
  83.                 // disable apply button
  84.                 $('#translator-preferences-button-apply').boolean('disabled', true);
  85.             }.bind(this));
  86.             
  87.             // cancel button
  88.             $('#translator-preferences-button-cancel').bind('command.translator', function(e) {
  89.                 window.close();
  90.             });
  91.             
  92.             // reset button
  93.             $('#translator-preferences-button-reset').bind('command.translator', function(e) {
  94.                 // reset all preferences
  95.                 this.prefs.resetAllPrefs();
  96.                 
  97.                 // refill UI interface
  98.                 this.fillUI();
  99.             }.bind(this));
  100.         },
  101.         
  102.         fillUI: function()
  103.         {
  104.             // load list of all supported languages
  105.             var $list = $('#translator-preferences-languages-list').eq(0).empty();
  106.             
  107.             $.each(namespace.Languages, function(code, name) {
  108.                 var $listitem = $(document.createElement('listitem'));
  109.                 
  110.                 $listitem.addClass('translator-preference');
  111.                 
  112.                 $listitem.attr('label', name);
  113.                 $listitem.attr('code', code);
  114.                 $listitem.attr('type', 'checkbox');
  115.                 
  116.                 $listitem.appendTo($list);
  117.             }.bind(this));
  118.             
  119.             
  120.             /* set all other preferences */
  121.             
  122.             $('#translator-preferences-translate-selection').boolean('checked', this.prefs.getPref('translate.selection'));
  123.             $('#translator-preferences-translate-floating').boolean('checked', this.prefs.getPref('translate.floating'));
  124.             $('#translator-preferences-translate-hotkey').boolean('checked', this.prefs.getPref('translate.hotkey'));
  125.             $('#translator-preferences-status-icon').boolean('checked', this.prefs.getPref('status.icon'));
  126.             $('#translator-preferences-status-label').boolean('checked', this.prefs.getPref('status.label'));
  127.             $('#translator-preferences-toolbar').boolean('checked', this.prefs.getPref('toolbar'));
  128.             $('#translator-preferences-context-menu').boolean('checked', this.prefs.getPref('context.menu'));
  129.             
  130.             // enable/disable hotkey textbox
  131.             $('#translator-preferences-hotkey, #translator-preferences-hotkey-note').boolean('disabled', (!this.prefs.getPref('translate.hotkey')));
  132.             
  133.             // set hotkey
  134.             $('#translator-preferences-hotkey')
  135.                 .val(this.formatHotKey(this.prefs.getPref('translate.hotkey.modifiers'), this.prefs.getPref('translate.hotkey.key')))
  136.                 .attr('hotkeymodifiers', this.prefs.getPref('translate.hotkey.modifiers'))
  137.                 .attr('hotkey', this.prefs.getPref('translate.hotkey.key'));
  138.             
  139.             // set selected languages
  140.             $('#translator-preferences-languages-list').children().each(function(i, listitem) {
  141.                 if(this.prefs.getPref('languages.selected').search(new RegExp('(^|,)' + $(listitem).attr('code') + '(,|$)', 'i')) >= 0) {
  142.                     $(listitem).attr('checked', true);
  143.                 }
  144.             }.bind(this));
  145.             
  146.             // disable apply button by default
  147.             $('#translator-preferences-button-apply').boolean('disabled', true);
  148.         },
  149.         
  150.         formatHotKey: function(modifiersString, keyString)
  151.         {
  152.             var keys = [];
  153.             var modifiers = modifiersString.split(' ');
  154.             
  155.             $.each(modifiers, function(i, modifier) {
  156.                 switch(modifier) {
  157.                     case 'control':
  158.                         keys.push('Ctrl');
  159.                         break;
  160.                         
  161.                     case 'shift':
  162.                         keys.push('Shift');
  163.                         break;
  164.                         
  165.                     case 'alt':
  166.                         keys.push('Alt');
  167.                         break;
  168.                         
  169.                     case 'meta':
  170.                         keys.push('Meta');
  171.                         break;
  172.                 }
  173.             });
  174.             
  175.             keys.push(keyString);
  176.             
  177.             return keys.join(' + ');
  178.         },
  179.         
  180.         savePreferences: function(preferences)
  181.         {
  182.             var preferences = {};
  183.             
  184.             /* gather all preferences */
  185.             
  186.             // selected languages list
  187.             preferences['languages.selected'] = [];
  188.             
  189.             $('#translator-preferences-languages-list').children().each(function(i, listitem) {
  190.                 if($(listitem).attr('checked') == 'true') {
  191.                     preferences['languages.selected'].push($(listitem).attr('code'));
  192.                 }
  193.             });
  194.             
  195.             // all other preferences
  196.             preferences['translate.selection'] = $('#translator-preferences-translate-selection').boolean('checked');
  197.             preferences['translate.floating'] = $('#translator-preferences-translate-floating').boolean('checked');
  198.             preferences['translate.hotkey'] = $('#translator-preferences-translate-hotkey').boolean('checked');
  199.             preferences['translate.hotkey.modifiers'] = $('#translator-preferences-hotkey').attr('hotkeymodifiers');
  200.             preferences['translate.hotkey.key'] = $('#translator-preferences-hotkey').attr('hotkey');
  201.             preferences['status.icon'] = $('#translator-preferences-status-icon').boolean('checked');
  202.             preferences['status.label'] = $('#translator-preferences-status-label').boolean('checked');
  203.             preferences['toolbar'] = $('#translator-preferences-toolbar').boolean('checked');
  204.             preferences['context.menu'] = $('#translator-preferences-context-menu').boolean('checked');
  205.             
  206.             // save preferences
  207.             $.each(preferences, function(k, v) {
  208.                 this.prefs.setPref(k, v);
  209.             }.bind(this));
  210.         }
  211.     };
  212. })(com.igorgladkov.translator, translatorJQuery);